package org.dslmagic.interpreter;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class StandardDslContext implements DslContext {

	Map<String, Stack<Object>> variables = new HashMap<String, Stack<Object>>();

	private Stack<Object> varStack(String name) {
		Stack<Object> stack = variables.get(name);
		if (stack == null) {
			stack = new Stack<Object>();
			variables.put(name, stack);
		}
		return stack;
	}

	public DslContext unset(String name) {
		varStack(name).pop();
		return this;
	}

	public Object get(String name) {
		Stack<Object> stack = varStack(name);
		return !stack.isEmpty() ? stack.peek() : null;
	}

	public DslContext set(String name, Object value) {
		varStack(name).push(value);
		return this;
	}

	public static DslContext create() {
		return new StandardDslContext();
	}

}
